package cn.chinaunicom.fast.common.exception;


import lombok.Getter;

/**
 *
 * @ProjectName:
 * @Package:        com.springboot.cloud.common.core.exception
 * @ClassName:      ServiceException
 * @Description:    业务自定义异常
 * @Author:         wangjc
 * @CreateDate:     2020/4/9 0009 11:09
 * @UpdateUser:     更新者
 * @UpdateDate:     2020/4/9 0009 11:09
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
@Getter
public class ServiceException extends RuntimeException {

    /**
     * 异常码
     */
    private String code;
    /**
     * 异常信息
     */
    private String msg;

    //TODO 对业务异常的返回码进行校验，规范到一定范围内
    public ServiceException(){
        this.code = ServiceErrorType.SERVICE_ERROR_TYPE.getCode();
        this.msg = ServiceErrorType.SERVICE_ERROR_TYPE.getMesg();
    }

    public ServiceException(ServiceErrorType errorType){
        setErrorType(errorType);
    }

    public ServiceException(String mesg){
        this.code = ServiceErrorType.SERVICE_ERROR_TYPE.getCode();
        this.msg = mesg;
    }

    public ServiceException(ServiceErrorType errorType, String message, Throwable cause){
        super(message, cause);
        setErrorType(errorType);
    }

    public ServiceException(ServiceErrorType errorType, String mesg){
        super(mesg);
        setErrorType(errorType);
        this.msg = mesg;
    }

    public ServiceException(String code, String msg){
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    private void setErrorType(ErrorType errorType){
        this.code = errorType.getCode();
        this.msg = errorType.getMesg();
    }

}
